<?php

namespace user\Controller;

/**
 * @property bool Login
 */
class LoginController extends UserAppController
{
    public function beforeFilter(){
        /*$session = $this->Session->is('AUTHE.USER_ID');
        if($session){
            $this->redirect(['module'=>'dashboard','controller'=>'index','action'=>'index']);
        }*/
    }

    public function indexAction(){

        $this->render('login');
    }

    public function loginAction(){
        $post = $this->request->post();

        if($this->request->is('post'))
        {
            if ($this->Login->isValidate($post))
            {
                $rs = $this->Login->setLogin($post);
                if (sizeof($rs) > 0)
                {
                    $this->createSession($rs[0]);
                    $this->redirect(['module' => 'dashboard', 'controller' => 'index', 'action' => 'index']);
                }
                else
                {
                    $this->set('data',$post);
                    $this->Session->setFlash('O usuário ou a senha inseridos estão incorretos.',['type'=>'danger']);
                    $this->redirect(['action' => 'index']);
                    return false;
                }
            }
            $this->returnMsg($this->Login->getMessage());
        }
    }

	/**
     * @param $param
     */
    private function createSession($param){
        $this->Session->write('AUTHE.USER_ID',$param->id);
        $this->Session->write('AUTHE.NAME',$param->name);
        $this->Session->write('AUTHE.USERNAME',$param->login);
    }
}